package com.zlx.sharelive.activity;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.UserAccountListBean;
import com.zlx.module_base.base_api.res_data.WithdrawBody;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提现
 */
public class WithdrawalAc extends BaseAc {

    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_bank)
    TextView tv_bank;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.et_money)
    EditText et_money;
    @BindView(R.id.tv_rate)
    TextView tv_rate;

    private BaseInfoBean mBaseInfoBean;
    private UserAccountListBean.InfoDTO mData;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_withdrawal;
    }

    @Override
    public void initViews() {
        super.initViews();

        getBaseInfo();
        getAgreement("0");
    }


    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                tv_content.setText(Html.fromHtml(data.getData().getInfo().get(0).getPost_content(), null, null));
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }


    /**
     * 用户基本信息
     */
    private void getBaseInfo() {
        ApiUtil.getUserApi().getBaseInfo(SPUtil.getToken(this)).
                observe(this, new BaseObserver<>(
                        new BaseObserverCallBack<ApiResponse<BaseInfoBean>>() {
                            @Override
                            public void onSuccess(ApiResponse<BaseInfoBean> data) {
                                if (null != data) {
                                    if (null != data.getData()) {
                                        if (data.getData().getCode() == 0) {
                                            mBaseInfoBean = data.getData();
                                            if (null != mBaseInfoBean.getInfo()) {
                                                if (mBaseInfoBean.getInfo().size() > 0) {
                                                    tv_rate.setText("(提现手续费" + data.getData().getInfo().get(0).getTx() + "%)");
                                                    if (StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getMoney())) {
                                                        tv_money.setText("0");
                                                    } else {
                                                        tv_money.setText(mBaseInfoBean.getInfo().get(0).getMoney());

                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();

                            }
                        }));
    }


    @OnClick({R.id.iv_back, R.id.tv_details, R.id.ll_bank_card, R.id.tv_entire, R.id.bt_comm})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_details:
                startActivity(new Intent(this, WithdrawalDetailsAc.class));
                break;
            case R.id.ll_bank_card:
                startActivityForResult(new Intent(this, BankCardListAc2.class), 20);
                break;
            case R.id.tv_entire:
                if(null!=mBaseInfoBean){
                    et_money.setText(mBaseInfoBean.getInfo().get(0).getMoney());
                }
                break;
            case R.id.bt_comm:
                if (StringUt.isEmpty(et_money.getText().toString().trim())) {
                    Toast.makeText(WithdrawalAc.this, "金额不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(tv_bank.getText().toString().trim())) {
                    Toast.makeText(WithdrawalAc.this, "请选择银行卡", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    withdraw();
                }
                break;
        }
    }

    private void withdraw() {
        WithdrawBody withdrawBody = new WithdrawBody();
        withdrawBody.setUserAccountId(mData.getId());
        withdrawBody.setMoney(et_money.getText().toString().trim());
        withdrawBody.setUserId((String) SPUtil.get(this, Constants.SP_ID, ""));
        ApiUtil.getMoneyApi().withdraw(SPUtil.getToken(WithdrawalAc.this),
                withdrawBody
        ).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
            @Override
            public void onSuccess(ApiResponse<BaseBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            Toast.makeText(WithdrawalAc.this, "提现成功", Toast.LENGTH_SHORT).show();
                            getBaseInfo();
                        } else {
                            Toast.makeText(WithdrawalAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data) {
            return;
        }
        switch (resultCode) {
            case 30:
                mData = (UserAccountListBean.InfoDTO) data.getSerializableExtra("data");
                tv_bank.setText(mData.getBankBranch());
                break;

        }
    }
}
